package consul

import (
	"fmt"
	"github.com/hashicorp/consul/api"
)

type consulClient struct {
	client *api.Client
}

func ConSul(address string, port int) (client *consulClient, err error) {
	defaultConfig := api.DefaultConfig()
	defaultConfig.Address = fmt.Sprintf("%s:%d", address, port)

	newClient, err := api.NewClient(defaultConfig)

	return &consulClient{client: newClient}, err

}

type ConsulRegister struct {
	ID      string
	Name    string
	Tags    []string
	Port    int
	Address string
}

func (c *consulClient) ServiceRegister(register ConsulRegister) {
	registration := api.AgentServiceRegistration{

		ID:      register.ID,
		Name:    register.Name,
		Tags:    register.Tags,
		Port:    register.Port,
		Address: register.Address,
	}

	c.client.Agent().ServiceRegister(&registration)

}
